package com.gitee.cirnochat.login.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.cirnochat.common.utils.IpInfoUtil;
import com.gitee.cirnochat.login.mapper.LoginHistoryMapper;
import com.gitee.cirnochat.login.model.dto.DeviceInfo;
import com.gitee.cirnochat.login.model.entity.IprangeInfo;
import com.gitee.cirnochat.login.model.entity.LoginHistory;
import com.gitee.cirnochat.login.model.vo.resp.LoginHistoryVo;
import com.gitee.cirnochat.login.service.IprangeInfoService;
import com.gitee.cirnochat.login.service.LoginHistoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @author FlanChan
 * @description 针对表【login_history(登陆历史)】的数据库操作Service实现
 * @createDate 2025-05-28 19:21:28
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class LoginHistoryServiceImpl extends ServiceImpl<LoginHistoryMapper, LoginHistory>
        implements LoginHistoryService {

    private final IprangeInfoService iprangeInfoService;
    @Async
    @Override
    public void saveLoginHistoryAsync(String deviceId, String ipAddress, DeviceInfo deviceInfo, String userAgent, int isLogin, Long userId) {
        IprangeInfo ipInfo = iprangeInfoService.selectIpInfoByIpAddress(ipAddress);
        LoginHistory loginHistory = new LoginHistory();
        loginHistory.setUserId(userId);
        loginHistory.setIpAddress(ipAddress);
        loginHistory.setIpDescription(IpInfoUtil.getIpDescription(ipInfo));
        loginHistory.setDeviceId(deviceId);
        loginHistory.setDeviceName(deviceInfo.getDeviceName());
        loginHistory.setDeviceType(deviceInfo.getDeviceType());
        loginHistory.setUserAgent(userAgent);
        loginHistory.setStatus(isLogin);
        this.save(loginHistory);
    }

    @Override
    public void cleanLoginHistory(int days) {
        LocalDate now = LocalDate.now();
        // 计算30天前的日期
        LocalDate beforeDate = now.minusDays(days);
        // 删除30天前的登录历史记录
        this.remove(new LambdaQueryWrapper<>(LoginHistory.class)
                .lt(LoginHistory::getLoginTime, beforeDate));
    }

    @Override
    public void clearLoginHistory(Long userId) {
        this.remove(new LambdaQueryWrapper<>(LoginHistory.class)
                .eq(LoginHistory::getUserId, userId));
    }

    @Override
    public void deleteLoginHistory(Long historyId, Long userId) {
       this.remove(new LambdaQueryWrapper<>(LoginHistory.class)
                .eq(LoginHistory::getId, historyId)
                .eq(LoginHistory::getUserId, userId));
    }

    @Override
    public List<LoginHistoryVo> getLoginHistory(int limitDays, Long userId) {
        LocalDate now = LocalDate.now();
        // 计算30天前的日期
        LocalDate beforeDate = now.minusDays(limitDays);
        return this.list(new LambdaQueryWrapper<>(LoginHistory.class)
                        .eq(LoginHistory::getUserId, userId)
                        .ge(LoginHistory::getLoginTime, beforeDate)
                        .orderByDesc(LoginHistory::getLoginTime))
                .stream().map(LoginHistoryVo::new).toList();
    }
}




